import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { School } from './entities/school.entity';
import { ApprovedSchoolDto, CreateSchoolDto, UpdateSchoolDto } from './dto/school.dto';
import { equals, isEmpty, isNil } from 'ramda';
import { throwHttp } from 'src/utlis';
import { SchoolStatus } from 'src/dictionaries/index.enum';
import { isTrue } from 'src/utlis/tools';
import { getTokenInfo } from 'src/common/auth/constants';

@Injectable()
export class SchoolService {
  constructor(
    @InjectRepository(School)
    private schoolRepository: Repository<School>,
    private readonly jwtService: JwtService,
  ) { }

  async create(schoolData: CreateSchoolDto, headers) {
    try {
      const user = getTokenInfo(this.jwtService, headers)
      const schoolInfo = await this.schoolRepository.findOne({
        where: { name: schoolData.name },
      });
      if (!isNil(schoolInfo)) {
        throwHttp('学校名称已存在');
        return
      }
      const school = new School()
      school.name = schoolData.name
      school.address = schoolData.address
      school.introduction = schoolData.introduction
      school.createById = user.userId
      const result = await this.schoolRepository.manager.save(school);
      if (!isEmpty(result)) {
        return `创建成功`;
      }
      throwHttp('创建失败');
    } catch (error) {
      throwHttp(error.message);
    }
  }
  async update(schoolData: UpdateSchoolDto, headers) {
    try {
      const user = getTokenInfo(this.jwtService, headers)
      const schoolInfo = await this.schoolRepository.findOne({
        where: { id: schoolData.id },
        select: ['id', 'status'],
      });
      if (!isTrue(schoolInfo)) {
        throwHttp('学校id不存在，请确认');
        return
      }
      const { affected } = await this.schoolRepository
        .createQueryBuilder()
        .update(School)
        .set({
          principalName: schoolData.principalName,
          introduction: schoolData.introduction,
          principalEmail: schoolData.principalEmail,
          address: schoolData.address,
          updateById: user.userId
        })
        .where('id = :id', { id: schoolData.id })
        .execute();
      if (affected) {
        return '更新成功';
      }
      throwHttp('更新失败');
    } catch (error) {
      throwHttp(error.message);
    }
  }
  async approved(schoolData: ApprovedSchoolDto, headers) {
    try {
      const user = getTokenInfo(this.jwtService, headers)
      const schoolInfo = await this.schoolRepository.findOne({
        where: { id: schoolData.id },
        select: ['id', 'status'],
      });
      if (!isTrue(schoolInfo)) {
        throwHttp('学校id不存在，请确认');
        return
      }
      const { affected } = await this.schoolRepository
        .createQueryBuilder()
        .update(School)
        .set({
          status: schoolData.status,
          updateById: user.userId
        })
        .where('id = :id', { id: schoolData.id })
        .execute();
      if (affected) {
        return '审核成功';
      }
      throwHttp('审核失败');
    } catch (error) {
      throwHttp(error.message);
    }
  }
}
